package common;

import java.util.ArrayList;

import Exception.SoapActionException;
import common.XmlParser;
import common.EDUBOX_Baese64_Crypto;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPBody;

import db.QueryManager;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class CommonEduSoap {
	ArrayList list = new ArrayList();
	SOAPMessage responseMessage = null;
	
	public CommonEduSoap(String lab_Type, String service_Type, String remoteAdrr) throws SOAPException, SoapActionException{
		
		InetAddress ia = null;
		try {
			ia = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//String remote_ip = ia.getHostAddress();
		String remote_ip=QueryManager.getQuery("local.ip");	
		
		MessageFactory messageFactory = MessageFactory.newInstance();
		SOAPMessage requestMessage = messageFactory.createMessage();
	        // http헤더 생성 
		javax.xml.soap.MimeHeaders headers = requestMessage.getMimeHeaders();
	    headers.setHeader("Content-Type", "text/xml; charset=utf-8");
	    headers.setHeader("SOAPAction","http://service.edubox.com/" + lab_Type);
	     
	     // SOAPPart 객체 생성
	    javax.xml.soap.SOAPPart soapPart = requestMessage.getSOAPPart();

	     //SOAPEnvelope 객체 생성
	    SOAPEnvelope envelope = soapPart.getEnvelope();

	     // 네임스페이스 지정
	    envelope.addNamespaceDeclaration("xsd" , "http://www.w3.org/2001/XMLSchema");
	    envelope.addNamespaceDeclaration("xsi" , "http://www.w3.org/2001/XMLSchema-instance");
	     
	     //SOAPFactory 객체 생성
	    SOAPFactory soapFactory = SOAPFactory.newInstance();
	        
	     
	     // SOAPBody 객체 얻기
	    SOAPBody body = envelope.getBody();

	     // 바디 엔트리 생성

	    // 이미 envelope에서 네임스페이스를 선언했으므로 마지막인자는 null 이다.
	    Name bodyName = soapFactory.createName(lab_Type, null, "http://service.edubox.com/");
	    SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

	        // 바디 엔트리 자식 엘리먼트 생성
	    Name sKeyName = soapFactory.createName("sKey");
	    SOAPElement sKeyNameNamechildElement = bodyElement.addChildElement(sKeyName);
	    sKeyNameNamechildElement.addTextNode(EDUBOX_Baese64_Crypto.Encode2("EDUBOX_EDUBOXHRD_01"));
//	    sKeyNameNamechildElement.addTextNode(EDUBOX_Baese64_Crypto.Encode("EDUBOX_EDUBOXHRD_01", "121.131.184.3"));
	        
	    Name sJobFlagName = soapFactory.createName("sJobFlag");
	    SOAPElement sJobFlagNamechildElement = bodyElement.addChildElement(sJobFlagName);
	    sJobFlagNamechildElement.addTextNode("LIST");


	    Name iPKIDchildName = soapFactory.createName("iPKID");
	    SOAPElement iPKIDchildNamechildElement = bodyElement.addChildElement(iPKIDchildName);
	    iPKIDchildNamechildElement.addTextNode("0");

	    Name iPageNochildName = soapFactory.createName("iPageNo");
	    SOAPElement iPageNochildElement = bodyElement.addChildElement(iPageNochildName);
	    iPageNochildElement.addTextNode("1");
	        
	    Name iPageSizechildName = soapFactory.createName("iPageSize");
	    SOAPElement iPageSizechildElement = bodyElement.addChildElement(iPageSizechildName);
	    iPageSizechildElement.addTextNode("4");
	        
	    Name sSearchTextchildName = soapFactory.createName("sSearchText");
	    SOAPElement sSearchTextchildElement = bodyElement.addChildElement(sSearchTextchildName);
	    sSearchTextchildElement.addTextNode("");
	    
        // SOAPConnection 객체생성
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = soapConnectionFactory.createConnection();

        // 요청 SOAP 메시지를 보내고, 응답 SOAP 메시지를 받는다.
        
        try{
        	responseMessage = connection.call(requestMessage,
        			"http://service.edubox.com/FreeZone/"+service_Type+".asmx?op="+lab_Type);
        }catch(Exception ee){
        	throw new SoapActionException("잘못된 경로를 호출하셨습니다. service_Type이나 lab_Type을 확인하세요!!!");
        }
	}
	
	public ArrayList soapList(){
        XmlParser xml = new XmlParser();
        list = xml.setXMLtoHashMap(responseMessage);
        
        return list;
	}
}
